package com.example.customrecordbar;

import java.io.IOException;

import com.example.customrecordbar.DrawView.OnBarMoveListener;
import com.example.customrecordbar.R;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener, MediaPlayer.OnPreparedListener, SurfaceHolder.Callback, OnCompletionListener {

	/**
	 * 自定义view
	 */
	private DrawView mDrawView;
	private Button mNormalBtn;
	private TextView mRecordTimeTextView;
	
	private SurfaceView mPreview;
	private SurfaceHolder holder;
	private MediaPlayer mp;
	private Handler handler = new Handler();
	private Utilities utils = new Utilities();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		setListener();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		synchronized (mp) {
			handler.removeCallbacks(mUpdateTimeTask);
			mp.release();
		}
	}

	private void initView() {
		mDrawView = (DrawView) findViewById(R.id.myview);
		mNormalBtn = (Button) findViewById(R.id.normal_button);
		mRecordTimeTextView = (TextView) findViewById(R.id.record_time);
		
		mPreview = (SurfaceView) findViewById(R.id.surface);
		holder = mPreview.getHolder();
		holder.addCallback(this);
		holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	private void setListener() {
		mNormalBtn.setOnClickListener(this);
		
		mDrawView.setOnBarMoveListener(new OnBarMoveListener() {
			
			@Override
			public void OnBarMove(long currentTime) {
				Log.d("zr", "currentTime = " + currentTime);
				mNormalBtn.setText("" + currentTime);
//				mRecordTimeTextView.setText("" + currentTime);
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.normal_button:
			mDrawView.getLeft();
			Log.d("zr", "mDrawView.getLeft() = " + mDrawView.getLeft());
			LayoutParams params = new LayoutParams(DeviceUtil
					.getScreenWidth(getApplicationContext())* 2, 200);
			mDrawView.setLayoutParams(params);
			break;
		default:
			break;
		}
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
//		setMeidaPlayer();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		
	}
	
	/**
	 * 设置播放的mediaplayer
	 */
	private void setMeidaPlayer() {
		Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.leesa);
		mp = new MediaPlayer();
		try {
			mp.setDataSource(getApplicationContext(), uri);
			mp.setDisplay(holder);
			mp.setOnPreparedListener(this);
			mp.setOnCompletionListener(this);
			mp.prepare();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 更新播放进度
	 */
	private void updateProgressBar() {
		handler.postDelayed(mUpdateTimeTask, 100);
	}   

	/**
	 * Background Runnable thread
	 * */
	private Runnable mUpdateTimeTask = new Runnable() {
		public void run() {
			try {
				synchronized (mp) {
					long totalDuration = mp.getDuration();
					long currentDuration = mp.getCurrentPosition();

					// Updating progress bar
					int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
					
					// 设置播放时间和进度
					mRecordTimeTextView.setText(utils.milliSecondsToTimer(currentDuration));
					mDrawView.setProgress(progress);
//					Log.d("zr", "progress = " + progress);

					// 延迟100ms运行线程
					handler.postDelayed(this, 100);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};

	@Override
	public void onPrepared(MediaPlayer mp) {
		mp.start();
		updateProgressBar();
	}

	@Override
	public void onCompletion(MediaPlayer mp) {
		synchronized (mp) {
			handler.removeCallbacks(mUpdateTimeTask);
			mp.release();
		}
	}
}
